# -*- coding:utf-8 -*-
from dao.MySqlDAL import MySqlDAL
import requests
from constant import constant_define
import re
__author__ = 'shudongma.msd(风骐)'

headers = {
        'Accept':'application/json, text/javascript,text/html */*; q=0.9',
        'Accept-Encoding':'gzip, deflate, sdch',
        'Accept-Language':'zh-CN,zh;q=0.8,en;q=0.6',
        'cache-control':'no-cache',
        'Connection':'keep-alive',
        'User-Agent': constant_define.user_agent,
        'Host':'data.ce.cn'
}

target_url = 'http://data.ce.cn/main/stock/csrs/index.shtml'

def init_stock_plate():
    req = requests.get(target_url,headers=headers)
    page_content = req.content
    plate_re = re.compile(r'<a href="(.+?)" target="_blank">\s+(.+?)\s+</a>')
    plate_dic = plate_re.findall(page_content)
    stock_info = []
    stock_re = re.compile(r'<td><div class="aleft"><a target="_blank" href=".+?">(\d{6})</a></div></td>')
    for row in  plate_dic:
        plate_detail = requests.get('http://data.ce.cn'+row[0]+'&numPerPage=3000',headers=headers).content
        stocklist = stock_re.findall(plate_detail)
        for item in stocklist:
            stock_info.append([row[1],row[0][-2:],item])

    sqlDao = MySqlDAL()
    res = sqlDao.get_dimensions_rows('select stock_id from tb_stock_info')
    stock_dict = {}
    for row in res:
        stock_dict[row['stock_id'][2:]] = row['stock_id']
    for vl in stock_info:
        if vl[2] not in stock_dict:
            # print vl[2],' not in stockinfo'
            continue
        vl[2] = stock_dict[vl[2]]
    print sqlDao.update_many_batch(['plate','plate_id'],stock_info,'tb_stock_info',['stock_id'],count=50)

init_stock_plate()
